package chapter10.eg13;

import chapter10.eg11.OrderFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class HttpXmlClientHandler extends SimpleChannelInboundHandler<HttpXmlResponse> {

  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    HttpXmlRequest request = new HttpXmlRequest(null, OrderFactory.create(123));
    ctx.writeAndFlush(request);
  }

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, HttpXmlResponse msg) throws Exception {
    System.out.println(
      "The client receive response of http header is: " + msg.getResponse().headers().names());
    System.out.println("The client receive response of http body is: " + msg.getResult());
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    cause.printStackTrace();
    ctx.close();
  }
}
